#!usr/bin/node

const http = require('http'),
      {join} = require('path'),
      fs = require("fs");

const buffer = {};

http.createServer((req,res) => {
    console.log(req.url);
    console.time('WD');

  let fileName = req.url.substring(1);
  fileName = join(__dirname,fileName);

  if(!buffer[fileName]) {
    if(!fs.existsSync(fileName)) {
      res.statusCode = 404;
      res.end('not found!');
      return ;
    }
    buffer[fileName] = fs.readFileSync(fileName);
  }

  console.timeEnd('WD');

  res.end(buffer[fileName]);
}).listen(8080);
